package com.mdh.runerrands.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.mdh.runerrands.Util.AlipayClient;
import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.*;
import com.mdh.runerrands.service.BaseUserService;
import com.mdh.runerrands.service.OrderLineService;
import com.mdh.runerrands.service.OrderService;
import com.mdh.runerrands.service.PayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping(value = "/controller/order")
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

   @Resource
   private BaseUserService baseUserService;


    @Resource
    private OrderLineService orderLineService;

    @ApiOperation("查询所有订单信息")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<Order> orderList = orderService.findAll();
            result.setData(orderList);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation(value = "通过ID删除")
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.deleteById(id);
            result.setData(b);
            if (b){
                result.setMessage("success");
            }else {
                result.setMessage("删除失败");
            }
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("通过Id查询订单的详细信息")
    @RequestMapping(value = "findById",method = RequestMethod.GET)
    public Result findById(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            OrderDetail orderDetail = orderService.findById(id);
            result.setData(orderDetail);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("查询订单信息并且订单级联关键的属性")
    @RequestMapping(value = "/listOrderLine",method = RequestMethod.GET)
    public Result query(Integer customerId,Integer waiterId,String status){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            List<OrderDetail> query = orderService.query(customerId, waiterId, status);
            result.setData(query);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation(value = "拒绝订单")
    @RequestMapping(value = "/rejectOrder",method = RequestMethod.GET)
    public Result rejectOrder(int id){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            Order order = new Order();
            order.setId(id);
            order.setStatus("待派单");
            boolean update = orderService.update(order);
            result.setData(update);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("批量删除订单")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.batchDelete(ids);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("派单")
    @RequestMapping(value = "/sendOrder",method = RequestMethod.GET)
    public Result sendOrder(int waiterId,int orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.sendOrder(orderId, waiterId);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("分页查询订单信息")
    @RequestMapping(value = "/queryPage",method = RequestMethod.POST)
    public Result queryPage(Integer page,Integer pageSize,Long orderTime,Integer total,String status,Integer customerId,Integer waiterId,Integer orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            QueryPage queryPage = new QueryPage();
            System.out.println(queryPage);
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            List<OrderDetail> list = orderService.queryPage(page, pageSize, orderTime, total, status, customerId, waiterId,orderId);
            queryPage.setTotal(orderService.selectCount(orderTime,total,status,customerId,waiterId,orderId));

            queryPage.setList(list);
            result.setData(queryPage);
            return result;
        }catch (Exception e){
            result.setData(JSON.toJSONString(e));
            System.out.println(JSON.toJSONString(e));
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("接单")
    @RequestMapping(value = "/takeOrder",method = RequestMethod.GET)
    public Result takeOrder(int orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.takeOrder(orderId);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation("员工服务结束")
    @RequestMapping(value = "/serviceCompleteOrder",method = RequestMethod.GET)
    public Result serviceCompleteOrder(int orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.serviceCompleteOrder(orderId);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("确认订单")
    @RequestMapping(value = "/confirmOrder",method = RequestMethod.GET)
    public Result confirmOrder(int OrderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.confirmOrder(OrderId);
            result.setMessage("success");
            result.setData(b);
            return result;
        }catch (Exception e){
            result.setData(JSON.toJSONString(e));
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("下单")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result save(int total,int customerId,int addressId,int productId,Integer addressId2,String note){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            Order order = new Order();
            order.setCustomerId(customerId);
            order.setStatus("待支付");
            Long time = System.currentTimeMillis();
            order.setOrderTime(time);
            order.setTotal(total);
            order.setAddressId(addressId);
            order.setAddressId2(addressId2);
            order.setNote(note);
            boolean insert = orderService.insert(order);
            OrderLine orderLine = new OrderLine();
            orderLine.setNumber(1);
            orderLine.setPrice(total);
            orderLine.setProductId(productId);
            orderLine.setOrderId(orderService.selectByTime(time).getId());
            orderLineService.insert(orderLine);
            result.setData(insert);
            result.setMessage("success");
            return result;
        }catch (Exception e){ result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }

    }
    @ApiOperation("取消订单")
    @RequestMapping(value = "/cancelOrder",method = RequestMethod.GET)
    public Result cancelOrder(Integer orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        try {
            boolean b = orderService.cancelOrder(orderId);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("支付")
    @RequestMapping (value = "/Pay",method = RequestMethod.POST)
    public Result aLiPay(Integer customerId, String totalAccount,Integer orderId){
        Result result = new Result();
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setStatus("200");
        result.setMessage("success");
        try {
            BaseUser user = baseUserService.findBaseUserById(customerId);
            Integer money=Integer.valueOf(user.getMoney())-Integer.valueOf(totalAccount);
            if (money < 0){
                result.setData("余额不足");
                return result;
            }
            BaseUser baseUser = new BaseUser();
            baseUser.setId(customerId);
            baseUser.setMoney(String.valueOf(money));
            int update = baseUserService.update(baseUser);
            boolean status = orderService.updateStatus(orderId, "待派单");
            if (update > 0 && status ){
                result.setData(true);
            }else {
                result.setData(false);
            }
            return result;
        }catch (Exception e){
            result.setData(e);
            result.setMessage("Exception");
            result.setStatus("500");
            return null;
        }

    }
}
